package com.nimitz.module.employee.service.impl;

import com.nimitz.common.annotation.DataScope;
import com.nimitz.common.constant.UserConstants;
import com.nimitz.common.utils.SecurityUtils;
import com.nimitz.common.utils.StringUtils;
import com.nimitz.module.employee.domain.EmpEmployee;
import com.nimitz.module.employee.domain.EmpInduction;
import com.nimitz.module.employee.mapper.EmpEmployeeMapper;
import com.nimitz.module.employee.mapper.EmpInductionMapper;
import com.nimitz.module.employee.service.IEmpEmployeeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 人员 业务层实现
 */
@Service
public class EmpEmployeeServiceImpl implements IEmpEmployeeService {

    @Resource
    private EmpEmployeeMapper employeeMapper;
    @Resource
    private EmpInductionMapper inductionMapper;

    /**
     * 新增人员
     *
     * @param employee 人员
     * @return 结果
     */
    @Override
    @Transactional
    public int insertEmployee(EmpEmployee employee) {
        // 新增人员
        int rows = employeeMapper.insertEmployee(employee);
        // 新增成功 生成入职记录
        if (rows > 0) {
            EmpInduction induction = new EmpInduction();
            induction.setEmployeeId(employee.getEmployeeId());
            induction.setCreateBy(SecurityUtils.getLoginUser().getUsername());
            inductionMapper.insertInduction(induction);
        }
        return rows;
    }

    /**
     * 批量删除人员
     *
     * @param updateBy    更新者
     * @param employeeIds 人员id集合
     * @return 结果
     */
    @Override
    public int deleteEmployeeByIds(String updateBy, Integer[] employeeIds) {
        return employeeMapper.deleteEmployeeByIds(updateBy, employeeIds);
    }

    /**
     * 修改人员
     *
     * @param employee 人员
     * @return 结果
     */
    @Override
    public int updateEmployee(EmpEmployee employee) {
        return employeeMapper.updateEmployee(employee);
    }

    /**
     * 根据人员id查询人员
     *
     * @param employeeId 人员id
     * @return 人员
     */
    @Override
    public EmpEmployee selectEmployeeById(Integer employeeId) {
        return employeeMapper.selectEmployeeById(employeeId);
    }

    /**
     * 根据条件查询人员列表（含分页）
     *
     * @param employee 人员
     * @return 人员集合
     */
    @Override
    @DataScope(deptAlias = "tn1")
    public List<EmpEmployee> selectEmployeeList(EmpEmployee employee) {
        return employeeMapper.selectEmployeeList(employee);
    }

    /**
     * 批量提交
     *
     * @param updateBy    更新者
     * @param employeeIds 人员ID集合
     * @return 结果
     */
    @Override
    public int submitEmployee(String updateBy, Integer[] employeeIds) {
        return employeeMapper.submitEmployee(updateBy, employeeIds);
    }

    /**
     * 批量驳回
     *
     * @param updateBy    更新者
     * @param employeeIds 人员ID集合
     * @return 结果
     */
    @Override
    public int rejectEmployee(String updateBy, Integer[] employeeIds) {
        return employeeMapper.rejectEmployee(updateBy, employeeIds);
    }

    /**
     * 批量审核
     *
     * @param updateBy     更新者
     * @param inductionIds 入职ID集合
     * @return 结果
     */
    @Override
    public int auditEmployee(String updateBy, Integer[] inductionIds) {
        return employeeMapper.auditEmployee(updateBy, inductionIds);
    }

    /**
     * 校验警号是否唯一
     *
     * @param employee 人员
     * @return 结果
     */
    @Override
    public boolean checkEmployeeCodeUnique(EmpEmployee employee) {
        int employeeId = StringUtils.isNull(employee.getEmployeeId()) ? -1 : employee.getEmployeeId();
        EmpEmployee info = employeeMapper.checkEmployeeCodeUnique(employee.getEmployeeCode());
        if (StringUtils.isNotNull(info) && info.getEmployeeId().intValue() != employeeId) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验联系电话是否唯一
     *
     * @param employee 人员
     * @return 结果
     */
    @Override
    public boolean checkEmployeePhoneUnique(EmpEmployee employee) {
        int employeeId = StringUtils.isNull(employee.getEmployeeId()) ? -1 : employee.getEmployeeId();
        EmpEmployee info = employeeMapper.checkEmployeePhoneUnique(employee.getEmployeePhone());
        if (StringUtils.isNotNull(info) && info.getEmployeeId().intValue() != employeeId) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验证件号码是否唯一
     *
     * @param employee 人员
     * @return 结果
     */
    @Override
    public boolean checkEmployeeIdCardUnique(EmpEmployee employee) {
        int employeeId = StringUtils.isNull(employee.getEmployeeId()) ? -1 : employee.getEmployeeId();
        EmpEmployee info = employeeMapper.checkEmployeeIdCardUnique(employee.getEmployeeIdCard());
        if (StringUtils.isNotNull(info) && info.getEmployeeId().intValue() != employeeId) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
